package com.linkai.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.linkai.dto.admin.business.*;
import com.linkai.dto.admin.business.inputOrderReback.RebackTableIndexDto;
import com.linkai.entity.*;
import com.linkai.mapper.*;
import com.linkai.service.InputorderService;
import com.linkai.service.SupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author yamon
 * @since 2021-02-27
 */
@Service
public class InputorderServiceImpl extends ServiceImpl<InputorderMapper, Inputorder> implements InputorderService {

    @Autowired
    private InputorderProductMapper inputorderProductMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private BstoreMapper bstoreMapper;

    @Autowired
    private MoneyaccountMapper moneyaccountMapper;


    @Autowired
    private BproductMapper bproductMapper;
    @Autowired
    private SupplierService supplierService;

    @Autowired
    private InputRebackOrderMapper inputRebackOrderMapper;
    /**
     * 保存一个进货单
     *
     * @param inputOrderJsonDto 进货单实体
     * @return flag
     */
    @Override
    public Integer saveInputOrder(InputOrderJsonDto inputOrderJsonDto) {
        final Inputorder entity = new Inputorder();
        entity.setInputOrderNumber(inputOrderJsonDto.getOrderId());
        entity.setSupId(inputOrderJsonDto.getSupplier());
        entity.setEmpId(inputOrderJsonDto.getEmployeeId());
        entity.setAccId(inputOrderJsonDto.getAccountMoeny());
        entity.setBstoId(inputOrderJsonDto.getStoreId());
        entity.setInpDate(inputOrderJsonDto.getInputDate());
        entity.setPrice(inputOrderJsonDto.getPrice());
        entity.setBack(inputOrderJsonDto.getBack());
        final int insert = baseMapper.insert(entity);
        if (insert==1){
            //保存成功.保存两个表
            final List<InputOrder4JsonDto> productIds = inputOrderJsonDto.getProductIds();
            productIds.forEach(productId->{
                final InputorderProduct inputorderProduct = new InputorderProduct();
                inputorderProduct.setProductId(productId.getBproId());
                inputorderProduct.setInputOrderId(entity.getId());
                inputorderProduct.setInpordNum(productId.getBproNum());
                inputorderProduct.setInpordPrice(productId.getBinputPrice());
                inputorderProduct.setEachSum(productId.getEachSum());
                inputorderProductMapper.insert(inputorderProduct);
            });
            return entity.getId();
        }
        return null;
    }

    /**
     * 首页进货单list
     *
     * @return list
     */
    @Override
    public List<InputOrderIndexDto> listAll() {
        return this.baseMapper.listAll();
    }

    /**
     * 查找一个更新dto
     *
     * @param id
     * @return
     */
    @Override
    public InputOrderUpdateDto selectInputOrderDto4UpdateById(Integer id) {
        //封装信息
        final Inputorder inputorder = baseMapper.selectById(id);
        InputOrderUpdateDto inputOrderUpdateDto = new InputOrderUpdateDto();
        inputOrderUpdateDto.setInputOrderNumber(inputorder.getInputOrderNumber());
        inputOrderUpdateDto.setId(inputorder.getId());
        inputOrderUpdateDto.setSupplier(supplierService.getById(inputorder.getSupId()));
        //查找
        //查找employee信息
        final Integer empId = inputorder.getEmpId();
        if (empId!=null){
            Employee employee = employeeMapper.selectById(empId);
            inputOrderUpdateDto.setEmployee(employee);
        }
        final Integer bstoId = inputorder.getBstoId();
        final Bstore bstore = bstoreMapper.selectById(bstoId);
        inputOrderUpdateDto.setBstore(bstore);
        inputOrderUpdateDto.setAccId(moneyaccountMapper.selectById(inputorder.getAccId()));
        inputOrderUpdateDto.setPrice(inputorder.getPrice());
        inputOrderUpdateDto.setInpDate(inputorder.getInpDate());
        inputOrderUpdateDto.setBack(inputorder.getBack());
        inputOrderUpdateDto.setInpState(inputorder.getInpState());
        //查找出来所有关联商品信息
        final QueryWrapper<InputorderProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("input_order_id",id);
        List<InputorderProduct> inputorderProducts = inputorderProductMapper.selectList(queryWrapper);
        inputorderProducts.forEach(inputorderProduct -> {
            final Bproduct bproduct = bproductMapper.selectById(inputorderProduct.getProductId());
            InputOrder4UpdateDto inputOrder4UpdateDto = new InputOrder4UpdateDto();
            inputOrder4UpdateDto.setBproUnit(bproduct.getBproUnit());
            inputOrder4UpdateDto.setBproTitle(bproduct.getBproTitle());
            inputOrder4UpdateDto.setBproId(inputorderProduct.getProductId());
            inputOrder4UpdateDto.setBproNum(inputorderProduct.getInpordNum());
            inputOrder4UpdateDto.setBinputPrice(inputorderProduct.getInpordPrice());
            inputOrder4UpdateDto.setEachSum(inputorderProduct.getEachSum());
            inputOrderUpdateDto.getBproducts().add(inputOrder4UpdateDto);
        });
        return inputOrderUpdateDto;
    }

    /**
     * 根据id删除
     *
     * @param id id
     * @return flag
     */
    @Override
    public Boolean removeInputOrderById(Integer id) {
        //先获取商品id
        final QueryWrapper<InputorderProduct> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("input_order_id",id);
        inputorderProductMapper.delete(queryWrapper);
        //删除进货单
        final int i = baseMapper.deleteById(id);
        return i != 0;
    }

    /**
     * 列出所有的进货单给进货退货单用
     * @return
     */
    @Override
    public List<RebackTableIndexDto> listAllInputOrder4Reback() {
        List<RebackTableIndexDto> rebackTableIndexDtos = new ArrayList<>();
        //先获得所有的inputorder信息
        final List<Inputorder> inputorders = baseMapper.selectList(null);
        inputorders.forEach(inputorder -> {
            RebackTableIndexDto rebackTableIndexDto = new RebackTableIndexDto();
            BeanUtils.copyProperties(inputorder,rebackTableIndexDto);
            rebackTableIndexDto.setSupplier(supplierService.getById(inputorder.getSupId()).getSupName());
            rebackTableIndexDto.setStoreName(bstoreMapper.selectById(inputorder.getBstoId()).getBstoTitle());
            final QueryWrapper<InputorderProduct> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("input_order_id",inputorder.getId());
            final List<InputorderProduct> inputorderProducts = inputorderProductMapper.selectList(queryWrapper);
            List<String> proNames = new ArrayList<>();
            for (InputorderProduct inputorderProduct : inputorderProducts) {
                //查商品
                proNames.add(bproductMapper.selectById(inputorderProduct.getProductId()).getBproTitle());
            }
            rebackTableIndexDto.setProName(proNames);
            rebackTableIndexDtos.add(rebackTableIndexDto);
        });
        return rebackTableIndexDtos;
    }
}
